<?php
class ContentValidateTask {
	private $_contentSpecs;
	private $_dbConnection;
	private $_languageId;

	public function __construct($databaseName = null, $languageId = 25) {
		$this->_dbConnection = new Database($databaseName);
		$this->_languageId = $languageId;
		$this->_contentSpecs = json_decode(file_get_contents('templates/avod.json'), true);
	}

	private function validateGenre($type) {
		$result = array();

		try {
			$genreSpec = $this->_contentSpecs[$type];
			$query = $genreSpec['query'];

			$pdoStmt = $this->_dbConnection->query($query, array(':language_id' => $this->_languageId, ':parent_id' => -1));
			$result = array();

			foreach ($pdoStmt as $val) {
				$result[] = $this->validate($val, $genreSpec);
			}
		}
		catch (Exception $e) {
			print_r($e);
		}
		catch (PDOException $pdoe) {
			print_r($pdoe);
		}

		return $result;
	}

	public function simpleValidate($task, $type) {
		$ret = array();

		if ($task['by_genre']) {
			$ret = $this->validateGenre($type . '_genre');
		}

		$byGenreSpec = $this->_contentSpecs[$type];
		$query = $byGenreSpec['query'];

		try {
			echo "{$type}_genre <br />\n";
			print_r($ret);

			foreach ($ret as $item => $val) {
				$genreId = array_keys($val);

				//TODO val for posters ...
				$pdoStmt = $this->_dbConnection->query($query, array($genreId[0], $this->_languageId));
				$result = array();

				foreach ($pdoStmt as $val) {
					$result[] = $this->validate($val, $byGenreSpec);
				}
				print_r($result);
			}
		}
		catch (Exception $e) {
			print_r($e);
		}
		catch (PDOException $pdoe) {
			print_r($pdoe);
		}

	}

	public function validateVod($task) {
		$byGenreSpec = $this->_contentSpecs['vod'];
		$query = $byGenreSpec['query'];
		echo $query;
		$pdoStmt = $this->_dbConnection->query($query, array(60001, $this->_languageId));
		$result = array();

		foreach ($pdoStmt as $val) {
			$result[] = $this->validate($val, $byGenreSpec);
		}
	}


	public function validateGame($task) {
		$byGenreSpec = $this->_contentSpecs['game'];
		$query = $byGenreSpec['query'];
		$pdoStmt = $this->_dbConnection->query($query, array(70000, $this->_languageId));
		$result = array();

		foreach ($pdoStmt as $val) {
			$result[] = $this->validate($val, $byGenreSpec);
		}
	}

	private function validate($data, $specs) {
		$spec = array();
		$result = array();

		$cms_id = 'psuedo';
		
		if (isset($data['cms_id'])) {
			$cms_id = $data['cms_id'];
		}
		else if (isset($data['_id'])) {
			$cms_id = $data['_id'];
		}

		foreach ($data as $key => $val) {
			$spec = array();
			if (isset($specs[$key]) && $specs[$key]['require']) {
				$spec = $specs[$key];

				$spec['status'] = 'KO';
				$val = trim($val);
				$spec['value'] = $val;
				$spec['message'] = '';

				/*
				if (is_array($spec) && isset($spec['dimension']) && $spec['dimension']['require']) {
					//TODO
				}
				 */
				$type = $spec['type'];

				$status = array();
				switch($type) {
					case 'string' : $status = $this->textHandler($spec, $val); break;
					case 'integer' : $status = $this->numericHandler($spec, $val); break;
					case 'jpg' :
					case 'png' :
					case 'gif' : $status = $this->mediaHandler($spec, $val); break;
					default: break;
				}
			}

			if (isset($status['status']) && isset($specs[$key])) {
				$spec['status'] = $status['status'];
				$spec['message'] = $status['message'];

				unset($spec['dimension']);
				$result[$cms_id][$key] = $spec;
			}
		}

		return $result;
	}

	private function textHandler($spec, $val) {
		$size = strlen($val);

		$max = isset($spec['count']) ? $spec['count'] : 1000;

		if (0 == $size) {
			$spec['message'] = 'This field should not be empty!';
		}
		else if ($size > $max) {
			$over = -($max - $size);
			$spec['message'] = "Character count should not longer than [{$max}] character(s).  [{$over}] character(s) over limit.";
		}
		else {
			$spec['status'] = 'OK';
		}

		return $spec;
	}

	private function mediaHandler($val) {
	}

	private function numericHandler($val) {
	}
}

?>
